#!/bin/sh
#
# Copyright (C) 2000-2020 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# This script will test prune algo on expired volumes
#
. scripts/functions

TestName="prune-expired-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

setup_shstore # simulate shared storage if enabled

start_test

echo $PWD/build/po > tmp/file-list

# We want to have pruning only when doing prune command
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AutoPrune", "No", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Volume Retention", "5s", "Pool")'

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
label storage=File volume=TestVolume002
run job=$JobName level=full yes
wait
@exec "touch $cwd/build/po/fr.po"
run job=$JobName level=incremental yes
wait
@$out $tmp/log2.out
@###############################
@# Will prune nothing
prune volume expired yes
list volume
@###############################
@# Back to log1
@$out $tmp/log1.out
update volume=TestVolume001 volstatus=Used
run job=$JobName level=full yes
wait
@exec "touch $cwd/build/po/fr.po"
run job=$JobName level=incremental yes
wait
messages
@sleep 6
@$out $tmp/log3.out
@###############################
@# will prune TestVolume001
prune volume expired yes
list volume
update volume=TestVolume002 volstatus=Used
@$out $tmp/log4.out
@###############################
@# will prune nothing
prune volume expired
no
list volume
@$out $tmp/log5.out
@###############################
@# will prune TestVolume002
prune volume expired
yes
list volume
quit
EOF

run_bacula

check_for_zombie_jobs storage=File
stop_bacula

check_two_logs

grep 'TestVolume001.*Append' $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should not prune TestVolume001 in $tmp/log2.out"
    estat=1
fi

grep 'TestVolume002.*Append' $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should not prune TestVolume002 in $tmp/log2.out"
    estat=1
fi

grep 'There are no more Jobs associated with Volume "TestVolume001"' $tmp/log3.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should prune TestVolume001 in $tmp/log3.out"
    estat=1
fi

grep 'TestVolume002.*Used' $tmp/log4.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should not TestVolume002 in $tmp/log4.out"
    estat=1
fi

grep 'There are no more Jobs associated with Volume "TestVolume002"' $tmp/log5.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should prune TestVolume002 in $tmp/log5.out"
    estat=1
fi

end_test
